import mysql from "mysql";

const conn = mysql.createConnection({
    host: "127.0.0.1",
    user: "root",
    password: "",
    database: "xindannet",
    port: 3306
});
conn.connect(function (err) {
    if (err) {
        console.log("连接失败1:" + err.message);
    }
});
function exec(sql, params, callback) {

    conn.query(sql, params, (err, result, fields) => {
        callback(err, result, fields)
    })
}

function execAsync(sql, params) {
    return new Promise((resolve, reject) => {
        conn.connect(function (err) {
            if (err) {
                console.log("连接失败2:" + err.message);
                // throw err;
                return
            }
        });
        conn.query(sql, params, (err, result, fields) => {
            if (err) {
                reject(err, result, fields);
                // throw err;
                return
            }
            resolve(result, fields);
        })
    })
}
module.exports = {
    exec,
    execAsync
}